package com.iretailer.report.web.business;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iretailer.report.base.BaseAction;
import com.iretailer.report.dto.PageResult;
import com.iretailer.report.dto.QueryOption;
import com.iretailer.report.dto.ReTagType;
import com.iretailer.report.model.TagType;
import com.iretailer.report.model.TagTypeMalls;
import com.iretailer.report.model.User;
import com.iretailer.report.util.RMap;
import com.iretailer.report.util.SecurityUserUtil;
import com.iretailer.report.util.StringUtil;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Auther: xjw
 * @Date: 2019/7/18 0018 11:58
 * @Description:
 */
@RestController
public class TagTypeController extends BaseAction {

    @RequestMapping("/business/gridTagType.action")
    public Map<String, Object> gridTagType(@RequestBody ReTagType rtt, HttpServletRequest request) {
        QueryOption queryOption = modelMapper.map(rtt, QueryOption.class);
        User user = SecurityUserUtil.currentUser(request);
        List<String> companyIds = null;
        if (!user.getRoleId().equals("-")) {
            companyIds = companyService.getTreeId(user.getCompanyId());
            companyIds.add(user.getCompanyId());
        }
        PageResult find = tagTypeService.gridTagType(companyIds, rtt.getCompanyName(), rtt.getName(), queryOption);
        return RMap.ok(find);
    }

    @RequestMapping("/business/gridMallTagType.action")
    public Map<String, Object> gridMallTagType(@RequestBody ReTagType rtt,HttpServletRequest request) {
        QueryOption queryOption = modelMapper.map(rtt, QueryOption.class);
         User user = SecurityUserUtil.currentUser(request);
        String companyId = user.getCompanyId();
        if (user.getRoleId().equals("-")) {
            companyId = null;
        }
        PageResult find = tagTypeMallsService.gridMallTagType(companyId, rtt.getCompanyName(), rtt.getMallName(), rtt.getName(), queryOption);
        return RMap.ok(find);
    }

    @RequestMapping("/business/saveOrUpdateTagType.action")
    public Map<String, Object> saveOrUpdateTagType(@RequestBody ReTagType rtt) {
        TagType tagType = modelMapper.map(rtt, TagType.class);
        if (StringUtil.empty(tagType.getCompanyId()) || StringUtil.empty(tagType.getName())) {
            return RMap.error("parameter is incorrect");
        }
        boolean result = tagTypeService.saveOrUpdate(tagType);
        return RMap.resultBoolean(result);
    }

    @RequestMapping("/business/deleteTagType.action")
    public Map<String, Object> deleteTagType(@RequestBody List<String> ids) {
        boolean result = tagTypeService.deleteByIds(ids);
        return RMap.resultBoolean(result);
    }

    @RequestMapping("/business/tagTypeSaveMalls.action")
    public Map<String, Object> tagTypeSaveMalls(@RequestBody ReTagType rtt) {
        boolean result = tagTypeService.saveTagTypeMalls(rtt.getId(), rtt.getMallIds());
        return RMap.resultBoolean(result);
    }

    @RequestMapping("/business/deleteTagTypeById_MallId.action")
    public Map<String, Object> deleteTagTypeById_MallId(@RequestBody ReTagType rtt) {
        boolean result = tagTypeMallsService.remove(new QueryWrapper<TagTypeMalls>().lambda().eq(TagTypeMalls::getTagTypeId, rtt.getId()).eq(TagTypeMalls::getMallId, rtt.getMallId()));
        return RMap.resultBoolean(result);
    }

    @RequestMapping("/business/selectTagTypeByCompanyId.action")
    public Map<String, Object> selectTagTypeByCompanyId(@RequestBody ReTagType rtt) {
        List<TagType> tagTypes = tagTypeService.list(new QueryWrapper<TagType>().lambda().eq(TagType::getCompanyId, rtt.getCompanyId()));
        return RMap.ok(tagTypes);
    }

    @RequestMapping("/business/saveTagTypeMall.action")
    public Map<String, Object> saveTagTypeMall(@RequestBody ReTagType rtt) {
        TagTypeMalls find = tagTypeMallsService.getOne(new QueryWrapper<TagTypeMalls>().lambda().eq(TagTypeMalls::getTagTypeId, rtt.getId()).eq(TagTypeMalls::getMallId, rtt.getMallId()));
        if (find != null && find.getId().equals(rtt.getId())) {
            return RMap.error(RMap.Code.DATAREPETITION, "data_duplication");
        } else {
            tagTypeMallsService.updateById(new TagTypeMalls(rtt.getMallTagTypeId(), rtt.getId(), rtt.getMallId()));
            return RMap.ok();
        }
    }
}
